package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.Exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    /*配套->公寓中间表*/
    @Autowired
    private ApartmentFacilityServiceImpl facilityService;
    /*图片*/
    @Autowired
    private GraphInfoService graphInfoService;
    /*杂费->公寓中间表*/
    @Autowired
    private ApartmentFeeValueService valueService;
    /*标签->公寓中间表*/
    @Autowired
    private ApartmentLabelService labelService;


    /*插入执行逻辑为先将原数据删除，在进行插入（缺点：效率低）*/
    @Override
    /*涉及到多张表要使用事务否则防止出现幻读、脏数据等事务问题*/
    @Transactional
    public void VoSaveOrUpdate(ApartmentSubmitVo apartmentSubmitVo) {
        /*判断是id是否存在，用来判断做save还是update*/
        boolean isUpdate = apartmentSubmitVo.getId() != null;
        /*注意：执行完save之后会自动将数据回显到形参中，包括id，所以要先进行判断isNull*/
        this.saveOrUpdate(apartmentSubmitVo);

        /*删除操作（注意这里都是对公寓表与对应的表的中间表操作）*/
        if (isUpdate) {
            /*1、图片*/
            /*根据公寓id进行*/
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>()
                    /*以公寓id进行删除对应的图片*/
                    .eq(GraphInfo::getItemId, apartmentSubmitVo.getId())
                    /*指定所属对象为：公寓*/
                    .eq(GraphInfo::getItemType, ItemType.APARTMENT));
            /*2、配套*/
            facilityService.remove(new LambdaQueryWrapper<ApartmentFacility>()
                    .eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId()));
            /*3、杂费*/
            valueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>()
                    .eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId()));
            /*4、标签*/
            labelService.remove(new LambdaQueryWrapper<ApartmentLabel>()
                    .eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId()));
        }

        /*插入操作*/
        /*1、图片*/
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        graphInfoService.insertByGraphVoAndItem(ItemType.APARTMENT, apartmentSubmitVo.getId(), graphVoList);
        /*2、配套*/
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        facilityService.insertByIdsAndItemId(apartmentSubmitVo.getId(), facilityInfoIds);
        /*3、杂费*/
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        valueService.insertByIdsAndItemId(apartmentSubmitVo.getId(), feeValueIds);
        /*4、标签*/
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        labelService.insertByIdsAndItemId(apartmentSubmitVo.getId(), labelIds);
    }

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Override
    public IPage<ApartmentItemVo> pageList(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.getPageList(page, queryVo);
    }

    /*图片*/
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    /*标签*/
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    /*配套*/
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    /*杂费*/
    @Autowired
    private FeeValueMapper feeValueMapper;

    @Override
    @Transactional
    /*查询详细信息*/
    public ApartmentDetailVo getDateById(Long id) {
        /*公寓信息*/
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        if (apartmentInfo == null) {
            return null;
        }
        /*图片列表*/
        List<GraphVo> graphVos = graphInfoMapper.getInfoByItem(ItemType.APARTMENT, id);
        /*标签列表*/
        List<LabelInfo> labelInfo = labelInfoMapper.getLabelInfo(id);
        /*配套列表*/
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.getInfoByItemId(id);
        /*杂费列表*/
        List<FeeValueVo> feeVList = feeValueMapper.getInfoByItemId(id);


        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphVos);
        apartmentDetailVo.setLabelInfoList(labelInfo);
        apartmentDetailVo.setFeeValueVoList(feeVList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);

        return apartmentDetailVo;
    }

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Override
    public void removeApartmentByid(Long id) {

        Long l = roomInfoMapper.selectCount(new LambdaQueryWrapper<RoomInfo>()
                .eq(RoomInfo::getApartmentId, id));
        if (l > 0) {
            throw new LeaseException(ResultCodeEnum.ADMIN_APARTMENT_DELETE_ERROR);
        }

        this.removeById(id);

        /*1、图片*/
        /*根据公寓id进行*/
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>()
                /*指定所属对象为：公寓*/
                .eq(GraphInfo::getItemId, ItemType.APARTMENT)
                /*以公寓id进行删除对应的图片*/
                .eq(GraphInfo::getItemId, id));
        /*2、配套*/
        facilityService.remove(new LambdaQueryWrapper<ApartmentFacility>()
                .eq(ApartmentFacility::getApartmentId, id));
        /*3、杂费*/
        valueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>()
                .eq(ApartmentFeeValue::getApartmentId, id));
        /*4、标签*/
        labelService.remove(new LambdaQueryWrapper<ApartmentLabel>()
                .eq(ApartmentLabel::getApartmentId, id));
    }
}




